# 
# choose(4,2) * choose(3,0) * choose(3,3)
# 
# choose(4,3) * choose(3,0) * choose(3,2)
# 
# choose(4,4) * choose(3,0) * choose(3,1)
# 
# choose(4,0) * choose(3,0) * choose(3,5)
# 
# choose(4,1) * choose(3,0) * choose(3,4)
library(dplyr)
library(ggpubr)

total_number <- choose(10, 5)

t1 <- data.frame(
  x = c(0,1, 2, 3)
)

t1 <- t1 %>%
  mutate(
    y0 = paste(choose(4, 0) * choose(3, x) * choose(3, 5 - x),total_number, sep='/'),
    y1 = paste(choose(4,1) * choose(3, x) * choose(3,  5- x-1),total_number, sep='/'),
    y2 = paste(choose(4,2) * choose(3, x) * choose(3,  5- x-2),total_number, sep='/'),
    y3 = paste(choose(4,3) * choose(3, x) * choose(3,  5- x-3),total_number, sep='/'),
    y4 = paste(choose(4,4) * choose(3, x) * choose(3,  5- x-4),total_number, sep='/')
  )

table1 <- ggtexttable(t1, rows = NULL, theme = ttheme("blank")) %>%
  tab_add_hline(at.row = 1:2, row.side = "top", linewidth = 2)
# Add horizontal and vertical lines
# tab %>%
#  tab_add_hline(at.row = c(1, 2), row.side = "top", linewidth = 3, linetype = 1) %>%
#  tab_add_hline(at.row = c(7), row.side = "bottom", linewidth = 3, linetype = 1) %>%
#  tab_add_vline(at.column = 2:tab_ncol(tab), column.side = "left", from.row = 2, linetype = 2)
#   
ggarrange(table1, ncol=1)
# 
# 
# library(ggplot2)
# library(patchwork)
# library(gridExtra)
# library(ggpubr)
# library(grid)
# # p1 <- ggplot(mtcars, aes(x = mpg)) + geom_histogram()
# # 
# # p2 <- ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()
# # 
# # 
# # table <- ggtext::ggtexttable(
# #   data.frame(
# #     `x` = c(1, 2, 3),
# #     `y` = c(4, 5, 6)
# #   ),
# #   rows = NULL
# # )
# 
# df <- head(mtcars, 10)
# 
# table <- tableGrob(df)
# 
# top_header <- textGrob("Table of mtcars dataset", gp=gpar(fontsize=20, fontface="bold"))
# tbl <- gtable::gtable_add_rows(table, heights = unit(1, "line"), pos = 0)
# tbl <- gtable::gtable_add_grob(tbl, top_header, t = 1, l = 1, r = ncol(df))
# 
# grid.newpage()
# grid.draw(tbl)
# 
# grid.arrange(table, ncol=1)
# 
# table1 <- ggtexttable(df)
# 
# ggarrange(table1, ncol=1)
# 
# 
# 

# Create a joint distribution table for the number of successes in two independent binomial trials

box <- c(rep("white",4), rep("red", 3), rep("black", 3))
rolls <- function() {
  balls <- sample(box, size=5, replace=FALSE)
  X <- sum(balls== "red")
  Y <- sum(balls== "white")
  
  c(X, Y)
}

results <- data.frame(t(replicate(10000, rolls())))
colnames(results) <- c("Red", "White")
table(results$Red, results$White)/1000
joint_dist <- results %>%
  group_by(Red, White) %>%
  summarise(Count = n(), .groups = 'drop') %>%
  mutate(Probability = Count / sum(Count))
print(joint_dist)
